using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace GameForKids
{

    public class Ghost : DrawableGameComponent
    {
        Texture2D Img;
        Vector2 position;
        Rectangle rect;
        public string str;
        float opacity = 1.0f;
        int effect = 1;
        protected SpriteFont font;
        public bool show = true;
        

        public Rectangle BoundingRectangle()
        {
            return rect;
        }

        public Ghost(Game game, Vector2 p, string s, int num): base(game)
        {
            Img = this.Game.Content.Load<Texture2D>("Textures/PacManGame/ghost"+num.ToString());
            font = this.Game.Content.Load<SpriteFont>("Fonts/HelpFont");
            position = p;
            rect = Img.Bounds;
            rect.X = (int)position.X;
            rect.Y = (int)position.Y;
            str = s;
        }


        public override void Initialize()
        {
            base.Initialize();
        }
        public virtual void Draw(SpriteBatch spriteBatch)
        {
            Vector2 rectOfText = font.MeasureString(str);
            float xScale = (rect.Width / rectOfText.X) * 0.85f;
            float yScale = (rect.Height / rectOfText.Y) * 0.85f;
            float scale = Math.Min(xScale, yScale);
            int strWidth = (int)Math.Round(rectOfText.X * scale);
            int strHeight = (int)Math.Round(rectOfText.Y * scale);
            Vector2 _postion = new Vector2();
            _postion.X = (((rect.Width - strWidth) / 2) + rect.X);
            _postion.Y = (((rect.Height - strHeight) / 2) + rect.Y);
            if (show)
            {
                spriteBatch.Draw(Img, rect, Color.White * opacity);
                spriteBatch.DrawString(font, str, _postion, Color.Black * opacity, 0.0f,
                    Vector2.Zero, scale, SpriteEffects.None, 0);
            }
        }
        public virtual void Update(GameTime gameTime)
        {
            if (effect == 1)
            {
                opacity -= 0.02f;
                if (opacity <= 0.4)
                    effect = 0;
            }
            else
            {
                opacity += 0.02f;
                if (opacity >= 0.9)
                    effect = 1;
            }

            base.Update(gameTime);
        }
    }
}
